package com.example.library_hy_advert;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.example.library_hy_advert.db.Advert;
import com.example.library_hy_advert.db.DaoManager;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class HYAdvertFragment extends Fragment {
    private static final String TAG = "HYAdvertFragment";
    private PublicityView publicityView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hy_advert,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        publicityView = view.findViewById(R.id.publicity_view);

        initData();
    }

    public void initData() {
        Log.d("play","广告 initData");
        UiMessageUtils.getInstance().addListener(PublicityManager.SET_CACHE,setCacheListener);
        UiMessageUtils.getInstance().addListener(PublicityManager.RESET,resetListener);
        UiMessageUtils.getInstance().addListener(PublicityManager.ADD_ADVERT,addAdvertListener);
        UiMessageUtils.getInstance().addListener(PublicityManager.REMOVE_ADVERT,removeAdvertListener);

        getLifecycle().addObserver(PublicityManager.getInstance());
    }

    private final UiMessageUtils.UiMessageCallback resetListener = localMessage -> {
        Log.d(TAG, "重置播放列表");
        publicityView.reset();
    };

    private final UiMessageUtils.UiMessageCallback addAdvertListener = localMessage -> {
        Advert advert = DaoManager.get().queryAdvertByNumber(localMessage.getObject().toString());
        Log.d(TAG, "插入数据：" + advert.getLocalPath());
        publicityView.addAdvert(advert);
    };

    private final UiMessageUtils.UiMessageCallback removeAdvertListener = localMessage -> {
        String advertNumber = localMessage.getObject().toString();
        Log.d(TAG, "删除数据：" + advertNumber);
        publicityView.removeAdvert(advertNumber);
    };

    private final UiMessageUtils.UiMessageCallback setCacheListener = localMessage -> {
        Log.d(TAG, "加载缓存");
        List<Advert> query = DaoManager.get().query(Advert.class);
        for (Advert advert : query) {
            if(FileUtils.isFileExists(advert.getLocalPath())){
                Log.d(TAG, "插入数据：" + advert.getLocalPath());
                publicityView.addAdvert(advert);
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getLifecycle().removeObserver(PublicityManager.getInstance());
    }
}
